/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.vineatoum.view.stateView;

import java.awt.GridBagLayout;
import javax.swing.JLabel;

import org.vineatoum.business.action.analyse.Prelevement;
import org.vineatoum.business.action.operationactive.Soutirage;
import org.vineatoum.business.state.Vin;
import org.vineatoum.controller.ActionController;
import org.vineatoum.controller.IActionController;
import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.observer.IObserverAction;
import org.vineatoum.view.action.PrelevementPanel;
import org.vineatoum.view.action.SoutiragePanel;
import org.vineatoum.view.component.JAccordion;
import org.vineatoum.view.component.TrackingPanel;
import org.vineatoum.view.utils.JPanelWrapped;
import org.vineatoum.view.utils.jtablecustom.JTableCustom;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

public class VinActionPanel extends JPanelWrapped implements IObserverAction {
	private static final long serialVersionUID = -5445564122111066285L;
	
	protected JLabel lblTitle;
	protected JLabel lblName;
	protected JLabel lblCuve;
	protected JLabel lblQte;
	protected JLabel lblAssemblages;
	
	protected JAccordion accordion;
	protected JPanel dataPanel;
	protected JPanel actionPanel;
	protected JTabbedPane tabPanel;
	protected TrackingPanel trackingPanel;
	protected JTableCustom<Prelevement> tablePrelevements;
	protected JTable tableRecoltes;
	
	protected JToolBar toolBarActions;
	
	protected JButton btnElevage;
	protected JButton btnOuillage;
	protected JButton btnBatonnage;
	protected JButton btnSO2;
	
	protected JButton btnSoutirage;
	
	protected JButton btnPreparation;
	protected JButton btnCollage;
	protected JButton btnFiltration;
	protected JButton btnStabilistation;
	
	protected JButton btnMiseBouteille;
	
	private IVineaToumModel model;
	private IActionController controller;

	/**
	 * Create the panel.
	 */
	public VinActionPanel(final IVineaToumModel model, final Vin vin) {
		this.model = model;
		this.model.addObserver(this);

		this.controller = new ActionController(model);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		dataPanel = new JPanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
				1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
		dataPanel.setLayout(gridBagLayout);
		
		{
			lblTitle = new JLabel("Vue vin");
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 0;
			dataPanel.add(lblTitle, gbc_lblVueMout);
		}
		{
			lblName = new JLabel("Nom : " + vin.getName());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 1;
			dataPanel.add(lblName, gbc_lblVueMout);
		}
		{
			lblCuve = new JLabel("Cuve: " + vin.getCuve());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 2;
			dataPanel.add(lblCuve, gbc_lblVueMout);
		}
		{
			lblQte = new JLabel("Quantité: " + vin.getQuantity());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 4;
			gbc_lblVueMout.gridy = 2;
			dataPanel.add(lblQte, gbc_lblVueMout);
		}
		{
			lblAssemblages = new JLabel("Assemblage: Récupérer la liste des vin qui compose ce vin");// + mout.getQuantity());
			GridBagConstraints gbc_lblVueMout = new GridBagConstraints();
			gbc_lblVueMout.insets = new Insets(0, 0, 5, 5);
			gbc_lblVueMout.gridx = 2;
			gbc_lblVueMout.gridy = 3;
			dataPanel.add(lblAssemblages, gbc_lblVueMout);
		}
		
		add(dataPanel);
		
		accordion = new JAccordion();
		{			
			tabPanel = new JTabbedPane();
			
			tablePrelevements = new JTableCustom<Prelevement>(new Prelevement()) {
				private static final long serialVersionUID = 1L;

				protected boolean fireEditingStarted(final JTableCustom<Prelevement> table,
						final int row, int column) {
					table.getCellEditor().stopCellEditing();
					final PrelevementPanel prelevementPanel = new PrelevementPanel(model,
							(Prelevement) table.getValueAt(row));
					Object[] options = { "OK", "Annuler" };
					int n = JOptionPane.showOptionDialog(getRootPane(), prelevementPanel,
							"Ajouter", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
							null, options, options[0]);
					if (n == JOptionPane.OK_OPTION) {

					}

					return false;
				}
			};

			// TODO
			List<Prelevement> prelevements = new ArrayList<Prelevement>();
//			for (Iterator iterator = vin.getAnalyses().iterator(); iterator.hasNext();) {
//				Analyse analyse = (Analyse) iterator.next();
//				prelevements.add((Prelevement) analyse);
//
//			}
			tablePrelevements.setData(prelevements);
			
			JPanel panel = new JPanel(new BorderLayout());
			panel.add(tablePrelevements, BorderLayout.CENTER);
			panel.add(tablePrelevements.getTableHeader(), BorderLayout.NORTH);
			
			tabPanel.addTab("Prélévement", panel);
			

			//TODO one table per action (or type of action)
			tableRecoltes = new JTable();
			JPanel panelRecoltes = new JPanel(new BorderLayout());
			panelRecoltes.add(tableRecoltes, BorderLayout.CENTER);
			panelRecoltes.add(tableRecoltes.getTableHeader(), BorderLayout.NORTH);

			tabPanel.addTab("Récoltes", panelRecoltes);
		}
		{
			btnSoutirage = new JButton("Faire un soutirage");
			btnSoutirage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					final SoutiragePanel soutiragePanel = new SoutiragePanel(model, new Soutirage());
					Object[] options = { "OK", "Annuler" };
					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
					if (n == JOptionPane.OK_OPTION) {
						//TODO implement soutirage
					}

				}
			});			
		}
		{
			btnElevage = new JButton("Elevage");
			btnElevage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});	
		}
		{
			btnOuillage = new JButton("Effectuer un Ouillage des futs");
			btnOuillage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});			
		}
		{
			btnBatonnage = new JButton("Effectuer un batonnage");
			btnBatonnage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});
		}
		{
			btnSO2 = new JButton("Relevé SO2");
			btnSO2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});
		}
		{
			btnPreparation = new JButton("Préparation du vin");
			btnPreparation.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});
		}
		{
			btnCollage = new JButton("Effectuer un collage");
			btnCollage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});
		}
		{
			btnFiltration = new JButton("Filtrer");
			btnFiltration.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});
		}
		{
			btnStabilistation = new JButton("Effectuer une Stabilisation");
			btnStabilistation.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});
		}
		{
			btnMiseBouteille = new JButton("Mettre en bouteille");
			btnMiseBouteille.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					final ElevagePanel elevagePanel = new ElevagePanel(model, new Soutirage());
//					Object[] options = { "OK", "Annuler" };
//					int n = JOptionPane.showOptionDialog(getRootPane(), soutiragePanel,
//							"Soutirage", JOptionPane.OK_CANCEL_OPTION,
//							JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
//					if (n == JOptionPane.OK_OPTION) {
//						
//					} //TODO

				}
			});
		}
		{
			toolBarActions = new JToolBar();
			toolBarActions.setOrientation(SwingConstants.VERTICAL);
			
			toolBarActions.add(btnSoutirage);
			toolBarActions.add(new JSeparator(SwingConstants.HORIZONTAL));
			toolBarActions.add(btnElevage);
			toolBarActions.add(btnOuillage);
			toolBarActions.add(btnBatonnage);
			toolBarActions.add(btnSO2);
			toolBarActions.add(new JSeparator(SwingConstants.HORIZONTAL));
			toolBarActions.add(btnPreparation);
			toolBarActions.add(btnCollage);
			toolBarActions.add(btnFiltration);
			toolBarActions.add(btnStabilistation);
			toolBarActions.add(new JSeparator(SwingConstants.HORIZONTAL));
			toolBarActions.add(btnMiseBouteille);
			
		}
		{
			actionPanel = new JPanel(new BorderLayout());
			
			actionPanel.add(tabPanel, BorderLayout.CENTER);
			actionPanel.add(toolBarActions, BorderLayout.WEST);
		}
		{
			trackingPanel = new TrackingPanel(vin);

			accordion.addBar("Action", actionPanel);
			accordion.addBar("Historique", trackingPanel);
			
			add(accordion, BorderLayout.CENTER);
		}
	}

	@Override
	public void doWhenWrapperCancelClick() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doWhenWrapperOkClick() {
		// TODO Auto-generated method stub

	}
	
	

}
